﻿using Little.Soldier.SWIFT.Validation;
using Little.Soldier.SWIFT.Validation.Block2;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Little.Soldier.SWIFT
{
  [Serializable]
  public class SwiftBlock2Input : SwiftBlock2
  {
    private string _DeliveryMonitoring;
    private string _ObsolescencePeriod;
    private string _DestinationAddress;

    public virtual string DeliveryMonitoring
    {
      get
      {
        return this._DeliveryMonitoring;
      }
      set
      {
        this._DeliveryMonitoring = value;
      }
    }

    public override string MessageType
    {
      get
      {
        return base.MessageType;
      }
      set
      {
        base.MessageType = value;
      }
    }

    public virtual string ObsolescencePeriod
    {
      get
      {
        return this._ObsolescencePeriod;
      }
      set
      {
        this._ObsolescencePeriod = value;
      }
    }

    public virtual string DestinationAddress
    {
      get
      {
        return this._DestinationAddress;
      }
      set
      {
        this._DestinationAddress = value;
      }
    }

    public override bool IsEmpty
    {
      get
      {
        switch (base.MessageType)
        {
          case null:
            if (this.DestinationAddress == null && (base.MessagePriority == null && this.DeliveryMonitoring == null))
              return this.ObsolescencePeriod == null;
            else
              break;
        }
        return false;
      }
    }

    public override bool IsInput
    {
      get
      {
        return true;
      }
    }

    public override string MessagePriority
    {
      get
      {
        return base.MessagePriority;
      }
      set
      {
        base.MessagePriority = value;
      }
    }

    public SwiftBlock2Input(SwiftMessage swiftMessage)
      : base(swiftMessage)
    {
    }

    public SwiftBlock2Input(SwiftMessage swiftMessage, string messageType, string destinationAddress, string messagePriority, string deliveryMonitoring, string obsolescencePeriod)
      : base(swiftMessage)
    {
      base.MessageType = messageType;
      this.DestinationAddress = destinationAddress;
      base.MessagePriority = messagePriority;
      this.DeliveryMonitoring = deliveryMonitoring;
      this.ObsolescencePeriod = obsolescencePeriod;
    }

    public override void InitializeRules()
    {
      this.Rules.Add((IValidate) new Validate_DestinationAddress(typeof (SwiftBlock2Input).GetProperty("DestinationAddress"), (object) this));
      this.Rules.Add((IValidate) new Validate_DeliveryMonitoring(typeof (SwiftBlock2Input).GetProperty("DeliveryMonitoring"), (object) this, true));
      this.Rules.Add((IValidate) new Validate_ObsolescencePeriod(typeof (SwiftBlock2Input).GetProperty("ObsolescencePeriod"), (object) this, true));
      this.Rules.Add((IValidate) new Validate_MessagePriority(typeof (SwiftBlock2Input).GetProperty("MessagePriority"), (object) this));
      base.InitializeRules();
    }

    public override bool Equals(object obj)
    {
      if (this != obj)
        goto label_28;
label_27:
      return true;
label_28:
      if (!base.Equals(obj) || this.GetType() != obj.GetType())
        return false;
      SwiftBlock2Input swiftBlock2Input = (SwiftBlock2Input) obj;
      if (this.DeliveryMonitoring == null)
      {
        if (swiftBlock2Input.DeliveryMonitoring != null)
          return false;
      }
      else if (this.DeliveryMonitoring != swiftBlock2Input.DeliveryMonitoring)
        return false;
      if (base.MessagePriority == null)
      {
        if (swiftBlock2Input.MessagePriority != null)
          return false;
      }
      else if (base.MessagePriority != swiftBlock2Input.MessagePriority)
        return false;
      if (base.MessageType == null)
      {
        if (swiftBlock2Input.MessageType != null)
          return false;
      }
      else if (base.MessageType != swiftBlock2Input.MessageType)
        return false;
      if (this.ObsolescencePeriod == null)
      {
        if (swiftBlock2Input.ObsolescencePeriod != null)
          return false;
      }
      else if (this.ObsolescencePeriod != swiftBlock2Input.ObsolescencePeriod)
        return false;
      if (this.DestinationAddress == null)
      {
        if (swiftBlock2Input.DestinationAddress != null)
          return false;
        else
          goto label_27;
      }
      else if (this.DestinationAddress != swiftBlock2Input.DestinationAddress)
        return false;
      else
        goto label_27;
    }

    public override int GetHashCode()
    {
      return 31 * (31 * (31 * (31 * (31 * base.GetHashCode() + (this.DeliveryMonitoring != null ? this.DeliveryMonitoring.GetHashCode() : 0)) + (base.MessagePriority != null ? base.MessagePriority.GetHashCode() : 0)) + (base.MessageType != null ? base.MessageType.GetHashCode() : 0)) + (this.ObsolescencePeriod != null ? this.ObsolescencePeriod.GetHashCode() : 0)) + (this.DestinationAddress != null ? this.DestinationAddress.GetHashCode() : 0);
    }

    internal override XmlNode ToXml(XmlDocument doc)
    {
      XmlElement element1 = doc.CreateElement("ApplicationHeaderBlock_Input");
      XmlElement element2 = doc.CreateElement("MessageType");
      element2.InnerText = this.MessageType;
      element1.AppendChild((XmlNode) element2);
      XmlElement element3 = doc.CreateElement("DestinationAddress");
      element3.InnerText = this.DestinationAddress;
      element1.AppendChild((XmlNode) element3);
      XmlElement element4 = doc.CreateElement("MessagePriority");
      element4.InnerText = this.MessagePriority;
      element1.AppendChild((XmlNode) element4);
      XmlElement element5 = doc.CreateElement("DeliveryMonitoring");
      element5.InnerText = this.DeliveryMonitoring;
      element1.AppendChild((XmlNode) element5);
      XmlElement element6 = doc.CreateElement("ObsolescencePeriod");
      element6.InnerText = this.ObsolescencePeriod;
      element1.AppendChild((XmlNode) element6);
      return (XmlNode) element1;
    }

    public override string GetValue()
    {
      if (this.IsEmpty)
        return (string) null;
      StringBuilder stringBuilder = new StringBuilder("I");
      if (base.MessageType != null)
        stringBuilder.Append(base.MessageType);
      if (this.DestinationAddress != null)
        stringBuilder.Append(this.DestinationAddress);
      if (base.MessagePriority != null)
        stringBuilder.Append(base.MessagePriority);
      if (this.DeliveryMonitoring != null)
        stringBuilder.Append(this.DeliveryMonitoring);
      if (this.ObsolescencePeriod != null)
        stringBuilder.Append(this.ObsolescencePeriod);
      return ((object) stringBuilder).ToString();
    }

    public override bool SetValue(string value, List<ValidationError> parseError)
    {
      bool flag = true;
      int length = value.Length;
      if (length < 16 && length <= 23)
      {
        if (parseError != null)
        {
          ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, ((object) new StringBuilder().Append("expected a string Value of 17 up to 23 chars and obtained a ").Append(length).Append(" chars string: '").Append(value).Append("'")).ToString());
          parseError.Add(validationError);
        }
        flag = false;
      }
      int index = 0;
      if (value.StartsWith("2:I"))
        index = 2;
      switch (length - index)
      {
        case 16:
        case 17:
        case 18:
        case 21:
          if (value[index].ToString().ToUpper() != "I")
          {
            if (parseError != null)
            {
              ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, "Value must match: I<mt><address>[<pri>[<monitoring>[<obsolescence>]]]");
              parseError.Add(validationError);
            }
            flag = false;
          }
          int start1 = index + 1;
          int size1 = 3;
          this.MessageType = this.GetValuePart(value, start1, size1);
          int start2 = start1 + size1;
          int size2 = 12;
          this.DestinationAddress = this.GetValuePart(value, start2, size2);
          int start3 = start2 + size2;
          int size3 = 1;
          this.MessagePriority = this.GetValuePart(value, start3, size3);
          int start4 = start3 + size3;
          int size4 = 1;
          this.DeliveryMonitoring = this.GetValuePart(value, start4, size4);
          int start5 = start4 + size4;
          int size5 = 3;
          this.ObsolescencePeriod = this.GetValuePart(value, start5, size5);
          int num = start5 + size5;
          return flag;
        default:
          if (parseError != null)
          {
            ValidationError validationError = new ValidationError(ValidationErrorType.UsupportedBlockFormat, "Value must match: I<mt><address>[<pri>[<monitoring>[<obsolescence>]]]");
            parseError.Add(validationError);
          }
          flag = false;
          goto case 16;
      }
    }
  }
}
